package com.mc2.docapp.disabilityassessmenttool;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputFilter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.mc2.docapp.disabilityassessmenttool.Misc.InputFilterMinMax;
import com.mc2.docapp.disabilityassessmenttool.SpinalAffection.C_kyphosis;

import java.util.HashSet;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;

import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.getEditTexts;
import static com.mc2.docapp.disabilityassessmenttool.utils.Statics.router;

public class Kyphosis extends AppCompatActivity {

    private static final int id = Activities.SPINAL_AFFECTION_KYPHOSIS;
    EditText kyphosisValue;
    EditText torsoImbalanceValue;
    private Realm realm;

    C_kyphosis c_kyphosis = new C_kyphosis();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kyphosis);
        initRealmDB();
        loadPrevData();


        kyphosisValue = (EditText) findViewById(R.id.kyphosisValue);
        torsoImbalanceValue = (EditText) findViewById(R.id.torsoImbalanceValue);
        sanitizeInputs();

        Button saveNnextButton = (Button) findViewById(R.id.saveAndNext);
        saveNnextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                c_kyphosis.setKyphosisValue(Integer.parseInt(IfNull(kyphosisValue.getText().toString(), "0")));
                c_kyphosis.setTorsoImbalance(Integer.parseInt(IfNull(torsoImbalanceValue.getText().toString(), "0")));

                c_kyphosis.calc(c_kyphosis);


                NewPatient.finalDto.getClass_spinal_affection().setC_kyphosis(c_kyphosis);

                realm.beginTransaction();
                realm.delete(C_kyphosis.class);
                realm.insert(c_kyphosis);
                realm.commitTransaction();

                Intent intent = new Intent(Kyphosis.this, router(Kyphosis.class));
                startActivity(intent);
            }
        });

        Button clearAll = (Button) findViewById(R.id.clear);
        clearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashSet<EditText> views = new HashSet<>();
                getEditTexts((ViewGroup) findViewById(R.id.activity_main), views);
                for (EditText h : views)
                    h.setText("");
            }
        });
    }


    public String IfNull(String value, String defaultValue) {
        if(value == null || value.isEmpty()) {
            return defaultValue;
        } else {
            return  value;
        }
    }

    public void sanitizeInputs() {
        kyphosisValue.setFilters(new InputFilter[]{new InputFilterMinMax("0", "60")});
        torsoImbalanceValue.setFilters(new InputFilter[]{new InputFilterMinMax("0", "32")});
    }

    private void initRealmDB() {
        Realm.init(getApplicationContext());
        // realm = Realm.getDefaultInstance();
        RealmConfiguration config = new RealmConfiguration.Builder().name(id+".realm").build();
        realm = Realm.getInstance(config);
    }

    private void loadPrevData() {
        final RealmResults<C_kyphosis> dtos = realm.where(C_kyphosis.class).findAll();
//        .equalTo("id",id).findAll();
        System.out.println();
        try {
            //apply ui
            //
            //mapUI(new C_kyphosis().strip(dtos.get(0)));
            //
            //realm.beginTransaction();
            //dtos.get(0).deleteFromRealm();
            //realm.commitTransaction();
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
            //no prev data found
        }
    }

    private void mapUI(C_kyphosis oldData) {
        kyphosisValue.setText(String.valueOf((long) (oldData.getKyphosisValue())));
        torsoImbalanceValue.setText(String.valueOf((long) (oldData.getTorsoImbalance())));
    }

}
